﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YTCMS.Core;
using YTCMS.Domain.Entities;
using YTCMS.Services.Services;

namespace Admin.Controllers
{
    public class ModuleController : BaseController
    {
        private readonly ImoduleService _module;
        public ModuleController(ImoduleService module)
        {
            this._module = module;
        }
        //
        // GET: /Module/
        public ActionResult Index(int? pageIndex = 1, int? pageSize = 10)
        {
            int pcount;
            var model = _module.GetMany(s => true, Order.desc, "sort");
            model = _module.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return View(model);
        }

        public ActionResult Search(string name, int? pageIndex = 1, int? pageSize = 10)
        {
            int pcount;
            var model = _module.GetMany(s => true, Order.desc, "sort");
            model = QueryHelper.WhereIf<module>(model, s => s.Name.Contains(name), !string.IsNullOrWhiteSpace(name));
            model = _module.PageList(model, pageIndex.Value - 1, pageSize.Value, out pcount);
            ViewBag.PageSize = pageSize.Value;
            ViewBag.TotalCount = pcount;
            return PartialView("List", model);
        }

        public ActionResult Add()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Add(module model)
        {
            if (ModelState.IsValid)
            {
                model.CreateTime = System.DateTime.Now;
                model.State = (int)CommonState.正常;
                _module.Add(model);
            }
            return new JsonResult() { Data = new { d = true } };
        }

        public ActionResult Edit(int id)
        {
            var model = _module.GetAsync(s => s.Id == id);
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(module model)
        {
            if (ModelState.IsValid)
            {
                _module.Update(model);
            }
            return new JsonResult() { Data = new { d = true } };
        }
        public ActionResult Details(int id)
        {
            var model = _module.GetAsync(s => s.Id == id);
            return View(model);
        }

        public ActionResult Delete(int id)
        {
            var model = _module.Get(s => s.Id == id);
            _module.Delete(model);
            return new JsonResult() { Data = true, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

	}
}